package com.rp.system.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.rp.common.utils.RpUtil;
import com.rp.log.annotation.RpLog;
import com.rp.system.dao.OrganDao;
import com.rp.system.po.Module;
import com.rp.system.po.Organ;


@Service
@Transactional
public class OrganService {

	@Autowired
	private OrganDao organDao;
	
	@RpLog(desc="添加组织机构")
	public String addOrgan(Organ organ){
		organ.setCreateDate(RpUtil.getCurrentDatetime());
		return (String) organDao.save(organ);
	}
	
	@RpLog(desc="更新组织机构")
	public void updateOrgan(Organ organ){
		organ.setUpdateDate(RpUtil.getCurrentDatetime());
		organDao.update(organ);
	}
	
	@RpLog(desc="删除组织机构")
	public void deleteOrgan(String id){
		organDao.delete(id);
	}
	
	@RpLog(desc="移动组织机构至根节点")
	public void moveOrgansToRoot(String[] ids){
		for (String id : ids) {
			Organ o = organDao.get(id);
			o.setPid(null);
		}
	}
	
	@RpLog(desc="移动组织机构")
	public void moveOrgans(String[] ids, String targetId) throws Exception{
		Organ targetOrgan = organDao.get(targetId);
		for (String id : ids) {
			Organ o = organDao.get(id);
			if(o.getId().equals(targetOrgan.getPid())){
				throw new Exception("目标节点不能是被移动节点的子节点");
			}else{
				o.setPid(targetId);
			}
		}
	}
	
	
	
	public OrganDao getOrganDao() {
		return organDao;
	}
	
}
